#region The MIT License
// The MIT License
// 
// Copyright (c) 2009 octalforty studios
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#endregion
using System;

namespace octalforty.Chinchillin.Actions.Db
{
    public class SqlServer2008ConnectionInfo : DbConnectionInfo, IEquatable<SqlServer2008ConnectionInfo>
    {
        public string ServerName
        { get; private set; }

        public int? Port
        { get; private set; }

        public SqlServer2008ConnectionInfo(DbConnectionInfo connectionInfo, string serverName, int? port) :
            this(connectionInfo.Login, connectionInfo.Password, serverName, port, connectionInfo.DatabaseName)
        {
        }

        public SqlServer2008ConnectionInfo(string login, string password, string serverName, int? port, string databaseName) : 
            base(login, password, databaseName)
        {
            ServerName = serverName;
            Port = port;
        }

        public override DbConnectionInfo Clone()
        {
            return new SqlServer2008ConnectionInfo(Login, Password, ServerName, Port, DatabaseName);
        }

        #region Object Members
        public bool Equals(SqlServer2008ConnectionInfo other)
        {
            if(ReferenceEquals(null, other)) return false;
            if(ReferenceEquals(this, other)) return true;
            return base.Equals(other) && Equals(other.ServerName, ServerName) && Equals(other.Port, Port);
        }

        public override bool Equals(object obj)
        {
            if(ReferenceEquals(null, obj)) return false;
            if(ReferenceEquals(this, obj)) return true;
            return Equals(obj as SqlServer2008ConnectionInfo);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                {
                    var result = (base.GetHashCode() * 397) ^ (ServerName != null ? ServerName.GetHashCode() : 0);
                    result = (result * 397) ^ (Port != null ? Port.Value.GetHashCode() : 0);
                    return result;
                }
            }
        }

        public override string ToString()
        {
            var server = Port.HasValue ?
                ServerName + "," + Port.Value :
                ServerName;

            return string.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3}", server, DatabaseName, Login, Password);
        }
        #endregion

        #region Operators
        public static bool operator ==(SqlServer2008ConnectionInfo left, SqlServer2008ConnectionInfo right)
        {
            return Equals(left, right);
        }

        public static bool operator !=(SqlServer2008ConnectionInfo left, SqlServer2008ConnectionInfo right)
        {
            return !Equals(left, right);
        }
        #endregion
    }
}